using System;
using Firestarter.Test.Base;
using FireStarterModeller.BO;
using Habanero.Base;
using Habanero.BO;
using Habanero.BO.ClassDefinition;
using NUnit.Framework;

namespace FireStarterModeller.Test.BO
{
    [TestFixture]
    public class TestInheritanceRelationshipDiscriminatorMustBeStringProp
    {
        [SetUp]
        public void SetupTest()
        {
            //Runs every time that any testmethod is executed
            TestUtilsDomain.SetupTest();
            BORegistry.DataAccessor = new DataAccessorInMemory();
        }

        [TestFixtureSetUp]
        public void TestFixtureSetup()
        {
            //Code that is executed before any test is run in this class. If multiple tests
            // are executed then it will still only be called once.
            TestUtilsDomain.SetupFixture();
        }

        [TearDown]
        public void TearDownTest()
        {
            //runs every time any testmethod is complete
        }

        #region TestInheritanceRelationshipDiscriminatorMustBeStringProp

        [Test]
        public void Test_CreateInheritanceRelationshipDiscriminator_MustBeStringProp()
        {
            //---------------Set up test pack-------------------
            DMProperty dmProperty = TestUtilsShared.CreateSavedDMProperty();
            TestUtilsDMProperty.SetDMPropIntType(dmProperty);
            DMInheritanceRelationship dmInheritanceRelationship  = new DMInheritanceRelationship();
            DMClass subClass = TestUtilsShared.CreateSavedDMClass(dmProperty.Class.Assembly);
            dmInheritanceRelationship.SubClass = subClass;
            dmInheritanceRelationship.Discriminator = dmProperty.PropertyName;
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            IBusinessObjectRule rule = new DMInheritanceRelationshipDiscriminatorMustBeStringProp();
            //---------------Test Result -----------------------
            Assert.IsNotNull(rule);
            Assert.AreEqual("DMInheritanceRelationshipDiscriminatorMustBeStringProp", rule.Name);
            Assert.AreEqual(ErrorLevel.Warning, rule.ErrorLevel);
        }


//        [Test]
//        public void Test_InheritanceRelationshipDiscriminatorMustBeStringProp_WhenInheritanceRelationshipNull_ShouldRaiseError()
//        {
//            //---------------Set up test pack-------------------
//            //---------------Assert Precondition----------------
//            //---------------Execute Test ----------------------
//            try
//            {
//                new DMInheritanceRelationshipDiscriminatorMustBeStringProp(null);
//                Assert.Fail("expected ArgumentNullException");
//            }
//            //---------------Test Result -----------------------
//            catch (ArgumentNullException ex)
//            {
//                StringAssert.Contains("Value cannot be null", ex.Message);
//                StringAssert.Contains("inheritanceRelationship", ex.ParamName);
//            }
//        }

        [Test]
        public void Test_IsValid_FailsIfNotStringPropertyType()
        {
            //---------------Set up test pack-------------------
            DMProperty dmProperty = TestUtilsShared.CreateSavedDMProperty();
            TestUtilsDMProperty.SetDMPropIntType(dmProperty);
            dmProperty.Save();
            DMInheritanceRelationship dmInheritanceRelationship = new DMInheritanceRelationship();
            DMClass subClass = TestUtilsShared.CreateSavedDMClass(dmProperty.Class.Assembly);
            dmInheritanceRelationship.SubClass = subClass;
            dmInheritanceRelationship.SuperClass = (DMClass) dmProperty.Class;
            dmInheritanceRelationship.Discriminator = dmProperty.PropertyName;
            subClass.Save();
            dmInheritanceRelationship.Save();
            IBusinessObjectRule rule = new DMInheritanceRelationshipDiscriminatorMustBeStringProp();
            //---------------Assert Precondition----------------
            Assert.AreEqual(ORMapping.SingleTableInheritance, dmInheritanceRelationship.InheritanceType);
            Assert.IsNotNull(dmInheritanceRelationship.SubClass);
            Assert.IsNotNull(dmInheritanceRelationship.Discriminator);
            Assert.AreNotEqual(DMPropertyType.PROP_TYPE_STRING, dmProperty.PropertyTypeString);
            //---------------Execute Test ----------------------
            bool isValid = rule.IsValid(dmInheritanceRelationship);
            //---------------Test Result -----------------------
            Assert.IsFalse(isValid);
            StringAssert.Contains("The discriminator for the superclass of '", rule.Message);
            StringAssert.Contains("'" + subClass.ToString() + "'", rule.Message);
            StringAssert.Contains("' must be a Property of type 'String' or a CustomType that can be resolved to string", rule.Message);
        }

        [Test]
        public void Test_IsValid_WhenCustomPropertyType_ShouldPass()
        {
            //---------------Set up test pack-------------------
            DMProperty dmProperty = TestUtilsShared.CreateSavedDMProperty();
            TestUtilsDMProperty.SetDMPropCustomType(dmProperty);
            dmProperty.Save();
            DMInheritanceRelationship dmInheritanceRelationship = new DMInheritanceRelationship();
            DMClass subClass = TestUtilsShared.CreateSavedDMClass(dmProperty.Class.Assembly);
            dmInheritanceRelationship.SubClass = subClass;
            dmInheritanceRelationship.SuperClass = (DMClass) dmProperty.Class;
            dmInheritanceRelationship.Discriminator = dmProperty.PropertyName;
            subClass.Save();
            dmInheritanceRelationship.Save();
            IBusinessObjectRule rule = new DMInheritanceRelationshipDiscriminatorMustBeStringProp();
            //---------------Assert Precondition----------------
            Assert.AreEqual(ORMapping.SingleTableInheritance, dmInheritanceRelationship.InheritanceType);
            Assert.IsNotNull(dmInheritanceRelationship.SubClass);
            Assert.IsNotNull(dmInheritanceRelationship.Discriminator);
            Assert.AreNotEqual("System", dmProperty.PropertyType.AssemblyName);
            //---------------Execute Test ----------------------
            bool isValid = rule.IsValid(dmInheritanceRelationship);
            //---------------Test Result -----------------------
            Assert.IsTrue(isValid);
        }

        [Test]
        public void Test_IsValid_PassesIfInheritanceTypeIsConcrete()
        {
            //---------------Set up test pack-------------------
            DMInheritanceRelationship dmInheritanceRelationship = new DMInheritanceRelationship();
            dmInheritanceRelationship.InheritanceType = ORMapping.ConcreteTableInheritance;
            IBusinessObjectRule rule = new DMInheritanceRelationshipDiscriminatorMustBeStringProp();
            //---------------Assert Precondition----------------
            Assert.AreEqual(ORMapping.ConcreteTableInheritance, dmInheritanceRelationship.InheritanceType);
            //---------------Execute Test ----------------------
            bool isValid = rule.IsValid(dmInheritanceRelationship);
            //---------------Test Result -----------------------
            Assert.IsTrue(isValid);
        }

        [Test]
        public void Test_IsValid_WhenClassTableInheritanceAndDiscrimatorEmpty_ShouldReturnTrue()
        {
            //---------------Set up test pack-------------------
            DMInheritanceRelationship dmInheritanceRelationship = new DMInheritanceRelationship();
            dmInheritanceRelationship.InheritanceType = ORMapping.ClassTableInheritance;
            IBusinessObjectRule rule = new DMInheritanceRelationshipDiscriminatorMustBeStringProp();
            //---------------Assert Precondition----------------
            TestUtilsShared.AssertStringEmpty(dmInheritanceRelationship.Discriminator, "dmInheritanceRelationship.Discriminator");
            //---------------Execute Test ----------------------
            bool isValid = rule.IsValid(dmInheritanceRelationship);
            //---------------Test Result -----------------------
            Assert.IsTrue(isValid);
        }

        [Test]
        public void Test_IsValid_PassesIfStringPropertyType()
        {
            //---------------Set up test pack-------------------
            DMProperty dmProperty = TestUtilsShared.CreateSavedDMProperty();
            TestUtilsDMProperty.SetDMPropStringType(dmProperty);
            dmProperty.Save();
            DMInheritanceRelationship dmInheritanceRelationship = new DMInheritanceRelationship();
            DMClass subClass = TestUtilsShared.CreateSavedDMClass(dmProperty.Class.Assembly);
            dmInheritanceRelationship.SubClass = subClass;
            dmInheritanceRelationship.SuperClass = (DMClass) dmProperty.Class;
            dmInheritanceRelationship.Discriminator = dmProperty.PropertyName;
            subClass.Save();
            dmInheritanceRelationship.Save();
            IBusinessObjectRule rule = new DMInheritanceRelationshipDiscriminatorMustBeStringProp();
            //---------------Assert Precondition----------------
            Assert.AreEqual(ORMapping.SingleTableInheritance, dmInheritanceRelationship.InheritanceType);
            Assert.IsNotNull(dmInheritanceRelationship.SubClass);
            Assert.IsNotNull(dmInheritanceRelationship.Discriminator);
            Assert.AreEqual(DMPropertyType.PROP_TYPE_STRING, dmProperty.PropertyTypeString);
            //---------------Execute Test ----------------------
            bool isValid = rule.IsValid(dmInheritanceRelationship);
            //---------------Test Result -----------------------
            Assert.IsTrue(isValid);
        }


        #endregion
    }

}